﻿#include "user_page_config.h"
#include "lv_drivers/win32drv/win32drv.h"


static lv_timer_t * timer;

int hour, minute, second = 0;


lv_obj_t* countdown_num_label; //倒计时数值更新

lv_obj_t* search_img;          //动态切换图片


const void* search_bg_img[] =
{

    [0] = (void *)&research0,
    [1] = (void*)&research1,
    [2] = (void*)&research2,
    [3] = (void*)&research3,
};



static const char  device_paired_char[][15] =
{
    "设备配对",
    "Device pairing",
};

static const char  countdown_char[][15] =
{
    "倒计时",
    "Countdown",
};



//秒转化为分钟 和s
void format_seconds(int second, int* hours, int* minutes, int* seconds) {
    *hours = second / 3600;
    *minutes = (second % 3600) / 60;
    *seconds = second % 60;
}





static lv_timer_cb_t paired_timer_Callback(lv_timer_t* timer)
{
    //printf("countdown--\r\n");
    page_config.Paired_Countdown--;

    lv_img_set_src(search_img, search_bg_img[page_config.Paired_Countdown%4]);//设置图片源

    format_seconds(page_config.Paired_Countdown, &hour, &minute, &second);

    if (page_config.language_type == CHINESE)
    {
        lv_label_set_text_fmt(countdown_num_label, "%d分%d秒", minute, second);
    }
    if (page_config.language_type == ENGLISH)
    {
        lv_label_set_text_fmt(countdown_num_label, "%d'%d\"", minute, second);
    }
}







static void pair_event_handler(lv_event_t* e)
{
    //printf("%s\r\n",__FUNCTION__);
    lv_event_code_t event = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);
    //lv_obj_t* obj = e->user_data;
    if (!lv_obj_is_valid(obj))
        return;
    if (event == LV_EVENT_DELETE)
    {
        printf("LV_EVENT_DELETE\r\n");

    }
    else if (event == LV_EVENT_CLICKED)
    {
        printf("%s LV_EVENT_CLICKED event=%d\r\n", __FUNCTION__,event);
        extern lv_obj_t* prj_prev_cont;
        extern lv_obj_t* prj_parent_cont;
        //printf("group = 0,lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));

        user_paired_page_exit(obj);
        //lv_obj_clean(prj_parent_cont);
        printf("prj_parent_cont lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(prj_parent_cont));
        prj_prev_cont = lv_obj_create(prj_parent_cont);


        //printf("lv_obj_get_state = %d\r\n", lv_obj_get_state(prj_prev_cont));

        user_setting_page_entry(prj_prev_cont);
    }

    else if (event == LV_EVENT_FOCUSED)
    {
        //lv_obj_add_state(obj, LV_STATE_FOCUS_KEY | LV_STATE_FOCUSED | LV_STATE_PRESSED);
        printf("LV_EVENT_FOCUSED event=%d\r\n", event);
    }
    else if (event == LV_EVENT_DEFOCUSED)
    {
        printf("LV_EVENT_DEFOCUSED event=%d\r\n", event);
    }
    else if (event == LV_EVENT_KEY)
    {
        printf("%s LV_EVENT_KEY event=%d\r\n", __FUNCTION__,event);
        extern lv_obj_t* prj_prev_cont;
        extern lv_obj_t* prj_parent_cont;
        //printf("group = 0,lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));
  
        user_paired_page_exit(obj);
        //lv_obj_clean(prj_parent_cont);
        printf("prj_parent_cont lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(prj_parent_cont));
        prj_prev_cont = lv_obj_create(prj_parent_cont);
        
        //printf("lv_obj_get_state = %d\r\n", lv_obj_get_state(prj_prev_cont));
        user_setting_page_entry(prj_prev_cont);
        //user_paired_page_entry(prj_prev_cont);
        //prj_prev_cont = lv_obj_create(prj_parent_cont);
        //user_setting_page_entry(prj_prev_cont);

    }else if (event == LV_EVENT_MENU_CHANGE)
    {
        //printf("LV_EVENT_MENU_CHANGE event=%d\r\n", event);
        uint32_t* PAGE_INDEX = lv_event_get_param(e);
        printf("%s LV_EVENT_MENU_CHANGE key code %d\r\n",__FUNCTION__, *PAGE_INDEX);

        page_config.current_page = *PAGE_INDEX;

        Menu_Switch_Callback(page_config.current_page, page_config.last_page);

    }
}



/*
    进入配对设置界面
*/
void user_paired_page_entry(lv_obj_t* obj)
{
    printf("%s\r\n", __FUNCTION__);
    page_config.current_page = PAIRED_MENU;

    lv_obj_set_style_pad_all(obj, 0, 0);
    lv_obj_set_style_border_width(obj, 0, 0);
    lv_obj_set_style_bg_color(obj, lv_color_black(), 0);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);


    // 背景图显示
    lv_obj_t* img_bg3 = lv_img_create(obj);
    lv_img_set_src(img_bg3, &bg3);//设置温度图片源
    lv_obj_set_align(img_bg3, LV_ALIGN_CENTER);

    lv_obj_t* img_bg2 = lv_img_create(obj);
    lv_img_set_src(img_bg2, &bg2);//设置温度图片源
    lv_obj_set_align(img_bg2, LV_ALIGN_CENTER);

    lv_obj_t* img_bg1 = lv_img_create(obj);
    lv_img_set_src(img_bg1, &bg1);//设置温度图片源
    lv_obj_set_align(img_bg1, LV_ALIGN_CENTER);



    lv_obj_t*  device_paired_label =  lv_label_create(obj);
    lv_label_set_recolor(device_paired_label, true);                        /*Enable re-coloring by commands in the text*/
    lv_obj_set_style_text_font(device_paired_label, &font18, 0);
    lv_obj_set_style_text_color(device_paired_label, lv_color_make(0xff, 0xff, 0xff), 0);
    lv_obj_align(device_paired_label, LV_ALIGN_TOP_MID, 0, 18);


    
    lv_obj_t* countdown_label = lv_label_create(obj);
    lv_label_set_recolor(countdown_label, true);                        /*Enable re-coloring by commands in the text*/
    lv_obj_set_style_text_font(countdown_label, &font18, 0);
    lv_obj_set_style_text_color(countdown_label, lv_color_make(0xff, 0xff, 0xff), 0);
    lv_obj_align(countdown_label, LV_ALIGN_TOP_MID, 0, 183);
    


    


    countdown_num_label = lv_label_create(obj);
    lv_label_set_recolor(countdown_num_label, true);                        /*Enable re-coloring by commands in the text*/
    lv_obj_set_style_text_font(countdown_num_label, &font18, 0);
    lv_obj_set_style_text_color(countdown_num_label, lv_color_make(0xff, 0xff, 0xff), 0);
    lv_obj_align(countdown_num_label, LV_ALIGN_BOTTOM_MID, 0, -10);




    page_config.Paired_Countdown = 1000;  //倒计时更新
    format_seconds(page_config.Paired_Countdown, &hour, &minute, &second);

    if (page_config.language_type == CHINESE)
    {
        lv_label_set_text(device_paired_label, device_paired_char[0]);
        lv_label_set_text(countdown_label, countdown_char[0]);

        lv_label_set_text_fmt(countdown_num_label, "%d分%d秒", minute, second);
        

    }
    if (page_config.language_type == ENGLISH)
    {
        lv_label_set_text(device_paired_label, device_paired_char[1]);
        lv_label_set_text(countdown_label, countdown_char[1]);
        lv_label_set_text_fmt(countdown_num_label, "%d'%d\"", minute, second);

    }





    search_img = lv_img_create(obj);  //显示动态图片

    lv_img_set_src(search_img, search_bg_img[0]);//设置图片源
    lv_obj_align(search_img, LV_ALIGN_CENTER, 0, 0);

    
    //lv_obj_set_pos(search_img, bat_pos[i].x, bat_pos[i].y);
  
    
  
    printf("----------%d:%d:%d----------", hour, minute, second);
    timer = lv_timer_create(paired_timer_Callback, 300, NULL);

    lv_obj_clear_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_event_cb(obj, pair_event_handler, LV_EVENT_CLICKED, obj);
    lv_obj_add_event_cb(obj, pair_event_handler, LV_EVENT_MENU_CHANGE, obj);
    lv_group_add_obj(g_group, obj);



}







/*
    退出释放内存
*/
void user_paired_page_exit(lv_obj_t* obj)
{

    lv_timer_del(timer);

    if (lv_group_get_obj_count(g_group) > 0)
    {
        printf("grpup num %d\r\n", lv_group_get_obj_count(g_group));
        lv_group_remove_all_objs(g_group);
    }
    else
    {
        printf("group = 0,lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));
    }

    page_config.last_page = page_config.current_page;
        
    lv_obj_clean((obj));


    //page_config.language_type = !page_config.language_type;
}
